import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '@/views/Login.vue'
import Main from '@/views/Main.vue'
import {useStore} from "vuex";
import {ElMessage} from "element-plus";

const routes = [
  // {path:'/',name:'',component:()=>import('')},
  {path:'/Test',name:'Test',component:()=>import('@/views/component-test/Test.vue')},
  {path:'/',name:'Login',component:Login},
  {path:'/Main',name:'Main',component:Main,
    redirect:'/DashBoard',
    children:[
      {path:'/DashBoard', name:'DashBoard', component:()=>import('@/views/DashBoard.vue')},
      {path:'/Room', name:'Room', component:()=>import('@/views/ums/room/Room.vue')},
      {path:'/RoomInsert',name:'RoomInsert',component:()=>import('@/views/ums/room/RoomInsert.vue')},
      {path:'/RoomUpdate',name:'RoomUpdate',component:()=>import('@/views/ums/room/RoomUpdate.vue')},
      {path:'/Dept',name:'Dept',component:()=>import('@/views/ums/dept/Dept.vue')},
      {path:'/DeptInsert',name:'DeptInsert',component:()=>import('@/views/ums/dept/DeptInsert.vue')},
      {path:'/DeptUpdate',name:'DeptUpdate',component:()=>import('@/views/ums/dept/DeptUpdate.vue')},
      {path:'/Emp',name:'Emp',component:()=>import('@/views/ums/emp/Emp.vue')},
      {path:'/EmpInsert',name:'EmpInsert',component:()=>import('@/views/ums/emp/EmpInsert.vue')},
      {path:'/EmpUpdate',name:'EmpUpdate',component:()=>import('@/views/ums/emp/EmpUpdate.vue')},
      {path:'/EmpRoles',name:'EmpRoles',component:()=>import('@/views/ums/emp/EmpRoles.vue')},
      {path:'/Personal',name:'Personal',component:()=>import('@/views/personal/Personal.vue')},
      {path:'/PersonalUpdate',name:'PersonalUpdate',component:()=>import('@/views/personal/PersonalUpdate.vue')},
      {path:'/Role',name:'Role',component:()=>import('@/views/ums/role/Role.vue')},
      {path:'/RoleInsert',name:'RoleInsert',component:()=>import('@/views/ums/role/RoleInsert.vue')},
      {path:'/RoleUpdate',name:'RoleUpdate',component:()=>import('@/views/ums/role/RoleUpdate.vue')},
      {path:'/Permission',name:'Permission',component:()=>import('@/views/ums/permission/Permission.vue')},    ]},
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

/**
 * 路由前置守卫： 每次转发路由前执行的函数
 * param to ： 来源地址
 * param from ： 目标地址
 * next ： 放行函数
 */
router.beforeEach((to, from, next) => {
  const vuex = useStore();
  // 登录页面放行
  if (to.path === '/' || vuex.state['loginFlag']){
    next();
  }else {
    ElMessage.warning('请先登录');
    setTimeout(()=> next('/'), 2000);
  }
})

export default router
